package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加菜品和口味
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void insertDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dish.setStatus(StatusConstant.DISABLE);

        dishMapper.insertDish(dish);

//        获取insert语句生成的主键值
        Long id = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
        }
//        向口味表插入n条数据
        dishFlavorMapper.insertFlavors(flavors);
    }

    /**
     * 分页查询
     *
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        Page<DishVO> dishVOS = dishMapper.pageQuery(pageQueryDTO.getName(), pageQueryDTO.getCategoryId(), pageQueryDTO.getStatus());
        return new PageResult(dishVOS.getTotal(), dishVOS.getResult());
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
//        判断当前菜品是否处于起售状态
        ids.forEach(id -> {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
//       判断当前菜品是否被套餐关联
        List<Long> setmeals = setmealDishMapper.getSetmealByDishIds(ids);
        if (setmeals != null && setmeals.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        ids.forEach(id -> {
//        删除菜品表中的菜品数据
            dishMapper.deleteById(id);
//        删除菜品关联的口味数据
            dishFlavorMapper.deleteByDishId(id);
        });
    }

    /**
     * 根据id查询菜品和口味
     *
     * @param id
     * @return
     */
    @Override
    public DishVO queryById(Long id) {
//        根据id查询菜品信息
        Dish dish = dishMapper.getById(id);
//        根据菜品id查询口味
        List<DishFlavor> dishFlavor = dishFlavorMapper.getByDishId(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavor);

        return dishVO;
    }

    /**
     * 修改菜品信息
     *
     * @param dishVO
     */
    @Override
    public void updateDish(DishVO dishVO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO, dish);

//        修改菜品表基本信息
        dishMapper.updateDish(dish);
//        删除原有的口味
        dishFlavorMapper.deleteByDishId(dishVO.getId());

//        重新插入口味数据
        List<DishFlavor> flavors = dishVO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dish.getId());
            });
            dishFlavorMapper.insertFlavors(flavors);
        }
    }

    /**
     * 起售或停售菜品
     *
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder().status(status).id(id).build();
        dishMapper.updateDish(dish);

//        判断当前状态是否停售菜品
        if (status == StatusConstant.DISABLE) {
//            根据菜品id获取包含其的套餐id
            List<Long> setmealIds = setmealDishMapper.getByDishId(id);
//            停售包含该菜品的套餐
            setmealIds.forEach(setmealId -> {
                Setmeal setmeal = Setmeal.builder().status(status).id(setmealId).build();
                setmealMapper.updateSetmeal(setmeal);
            });
        }
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> getByType(Long categoryId) {
        List<Dish> dishes = dishMapper.getByType(categoryId);
        return dishes;
    }

    /**
     * 根据分类id查询菜品
     *
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> list(Dish dish) {
        List<Dish> dishes = dishMapper.list(dish.getCategoryId());
        ArrayList<DishVO> dishVOS = new ArrayList<>();

        dishes.forEach(dishs -> {
            DishVO dishWithFlavor = new DishVO();
            BeanUtils.copyProperties(dishs, dishWithFlavor);

            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(dishWithFlavor.getId());
            dishWithFlavor.setFlavors(flavors);
            dishVOS.add(dishWithFlavor);
        });

        return dishVOS;
    }
}
